package evi.com.evi.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ScrollView;
import android.widget.TextView;

import evi.com.evi.R;

/**
 * @author MY
 * @TODO 选择样式1:展示一些列表数据:(spinner)
 */
public class PopStringList {

    private static PopupWindow popupWindow; // 必须全局：所有方法操作
    private View dropdown;
    private int type = 0;    //0-从下往上， 1-从上往下,左边显示， 2-从上往下,右边显示

    public PopStringList() {

    }

    public interface Stringcallback {
        void sure(String chosed, String id);
    }

    /*
     * 仅返回String
     */
    public void getStringlist(Activity activity, String title,
                              String[] content, final Stringcallback callback) {
        getStringlist(activity, title, content, null, callback);
    }

    public PopStringList(FragmentActivity activity, String title, String[] content, String[] id, Stringcallback callback, int type, View view) {
        this.type = type;
        this.dropdown = view;
        getStringlist(activity, title, content, id, callback);
    }

    /*
     * 返回string 和对应id
     */
    public void getStringlist(Activity activity, String title,
                              String[] content, String[] id, final Stringcallback callback) {
        if (!(popupWindow == null || !popupWindow.isShowing()))
            return;
        View view = getView(activity, title, content, id, callback);

        popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT, true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        if (type == 0) {
            popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
        } else {
            popupWindow.showAsDropDown(dropdown);
        }
    }

    private View getView(Activity activity, String title, String[] content,
                         String[] id, Stringcallback callback) {

        View view;
        if (type == 0) {
            view = activity.getLayoutInflater().inflate(R.layout.pri_stringlist, null);
        } else {
            view = activity.getLayoutInflater().inflate(R.layout.pri_stringlist2, null);
            View view_left = (View) view.findViewById(R.id.view_left);
            View view_right = (View) view.findViewById(R.id.view_right);
            if(type == 1){
                view_left.setVisibility(View.GONE);
                view_right.setVisibility(View.VISIBLE);
            }else if(type == 2){
                view_left.setVisibility(View.VISIBLE);
                view_right.setVisibility(View.GONE);
            }
        }

        view.findViewById(R.id.bg).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                popupWindow = null;
            }
        });
        ((TextView) view.findViewById(R.id.title)).setText(title); // title
        if (content.length <= 5) {
            ScrollView scroll = (ScrollView) view.findViewById(R.id.scroll);
            LayoutParams layoutParams = scroll.getLayoutParams();
            layoutParams.height = layoutParams.WRAP_CONTENT;
            scroll.setLayoutParams(layoutParams);
        }

        LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll); // 内容容器
        for (int i = 0; i < content.length; i++) {
            ll.addView(getTextView(activity, content[i], id != null ? id[i] : "", callback), new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        }
        return view;
    }

    /**
     * @TODO 基本样式view
     */
    private View getTextView(Context context, final String s, final String id, final Stringcallback callback) {
        LinearLayout ll = (LinearLayout) View.inflate(context, R.layout.pri_stringitem, null);
        TextView text = (TextView) ll.findViewById(R.id.text);
        text.setText(s);
        text.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                callback. sure(s, id);
                popupWindow.dismiss();
                popupWindow = null;
            }
        });
        return ll;
    }
}
